import axios from 'axios'
import { Message } from 'element-ui';
// import Vue from 'vue'

const http = axios.create({
  baseURL: 'http://localhost:3000/admin/api',
  timeout: 25000 // request timeout
})

// Add a request interceptor
http.interceptors.request.use(function (config) {
  // Do something before request is sent
  if (sessionStorage.token) {
    config.headers.Authorization = 'Bearer ' + sessionStorage.token
  }

  return config;
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

// Add a response interceptor
http.interceptors.response.use(function (response) {
  // Any status code that lie within the range of 2xx cause this function to trigger
  // Do something with response data
  return response;
}, function (error) {
  // Any status codes that falls outside the range of 2xx cause this function to trigger
  // Do something with response error
  Message.error(error.response.data)
  // if (error.response.data) {
  //   this.$message.error(error.response.data)
  // }
  // return Promise.reject(error);
  // this.$message.error
});

export default http